package com.lyk.coursearrange.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyk.coursearrange.common.ServerResponse;
import com.lyk.coursearrange.entity.CourseInfo;
import com.lyk.coursearrange.entity.CoursePlan;
import com.lyk.coursearrange.entity.CoursePlanWithTeacherCourse;
import com.lyk.coursearrange.service.CoursePlanWithTeacherCourseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * VIEW 前端控制器
 * </p>
 *
 * @author lequal
 * @since 2023-04-26
 */
@RestController
@RequestMapping("/coursePlanWithTeacherCourse")
public class CoursePlanWithTeacherCourseController {

    @Autowired
    CoursePlanWithTeacherCourseService coursePlanWithTeacherCourseService;
   // @GetMapping("/getAllByInfo/{page}")
//    public ServerResponse getCoursePlanByInfo(@PathVariable("page") Integer page,@RequestParam(defaultValue = "10") Integer limit){
//        Page<CoursePlanWithTeacherCourse> pages = new Page<>(page,limit);
//        QueryWrapper<CoursePlanWithTeacherCourse> wrapper = new QueryWrapper<>();
//        IPage<CoursePlanWithTeacherCourse> ipage = coursePlanWithTeacherCourseService.page(pages,wrapper);
//        if (ipage != null) {
//            return ServerResponse.ofSuccess(ipage);
//        }
//        return ServerResponse.ofError("查询课程失败！");
//    }
    @GetMapping("/getAllByInfo/{page}")
    public ServerResponse getAllCourse(@PathVariable("page") Integer page,
                                       @RequestParam(defaultValue = "10") Integer limit) {
        Page<CoursePlanWithTeacherCourse> pages = new Page<>(page, limit);
        QueryWrapper<CoursePlanWithTeacherCourse> wrapper = new QueryWrapper<CoursePlanWithTeacherCourse>();
        IPage<CoursePlanWithTeacherCourse> ipage = coursePlanWithTeacherCourseService.page(pages, wrapper);

        if (ipage != null) {
            return ServerResponse.ofSuccess(ipage);
        }
        return ServerResponse.ofError("查询课程失败！");
    }


    /**
     * 关键字查询排课信息
     * @param page
     * @param limit
     * @param keyword
     * @return
     */
    @GetMapping("/search/{page}/{keyword}")
    public ServerResponse searchCourseInfo(@PathVariable("page") Integer page,
                                           @RequestParam(defaultValue = "10") Integer limit,
                                           @PathVariable("keyword") String keyword) {
        QueryWrapper<CoursePlanWithTeacherCourse> wrapper = new QueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(keyword), "course_name", keyword);
        Page<CoursePlanWithTeacherCourse> pages = new Page<>(page, limit);
        IPage<CoursePlanWithTeacherCourse> iPage = coursePlanWithTeacherCourseService.page(pages, wrapper);
        return ServerResponse.ofSuccess(iPage);
    }
    @GetMapping("/search1/{page}")
    public ServerResponse searchCourseInfo(@PathVariable("page") Integer page,
                                           @RequestParam(defaultValue = "10") Integer limit
                                           ) {
        QueryWrapper<CoursePlanWithTeacherCourse> wrapper = new QueryWrapper<>();
        Page<CoursePlanWithTeacherCourse> pages = new Page<>(page, limit);
        IPage<CoursePlanWithTeacherCourse> iPage = coursePlanWithTeacherCourseService.page(pages, wrapper);
        return ServerResponse.ofSuccess(iPage);
    }
    /**
     * 关键字查询排课信息
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/searchByClass/{semester}/{classNo}/{page}")
    public ServerResponse searchCourseInfoByClass(@PathVariable("semester") String semester,
                                                  @PathVariable("classNo") String classNo,
                                                  @PathVariable("page") Integer page,
                                           @RequestParam(defaultValue = "25") Integer limit
                                           ) {
        QueryWrapper<CoursePlanWithTeacherCourse> wrapper = new QueryWrapper<CoursePlanWithTeacherCourse>().eq("semester",semester).eq("class_no",classNo).orderByAsc("class_time");
        //wrapper.like(!StringUtils.isEmpty(keyword), "course_name", keyword);
        Page<CoursePlanWithTeacherCourse> pages = new Page<>(page, limit);
        IPage<CoursePlanWithTeacherCourse> iPage = coursePlanWithTeacherCourseService.page(pages, wrapper);
        return ServerResponse.ofSuccess(iPage);
    }

    /**
     * 根据ID删除教材信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ServerResponse deleteCourseInfoById(@PathVariable("id") Integer id) {
        boolean b = coursePlanWithTeacherCourseService.removeById(id);
        if (b) {
            return ServerResponse.ofSuccess("删除成功");
        }
        return ServerResponse.ofError("删除失败");
    }

    /**
     * 更新排课信息
     * @param id
     * @param courseInfo
     * @return
     */
    @PostMapping("/modify/{id}")
    public ServerResponse modifyCourseInfo(@PathVariable("id") Integer id, @RequestBody CoursePlanWithTeacherCourse courseInfo) {
        QueryWrapper<CoursePlanWithTeacherCourse> wrapper = new QueryWrapper<CoursePlanWithTeacherCourse>().eq("id", id);
        boolean b = coursePlanWithTeacherCourseService.update(courseInfo, wrapper);
        if (b) {
            return ServerResponse.ofSuccess("更新成功");
        }
        return ServerResponse.ofError("更新失败");
    }

}

